#include "managerwidget.h"
#include "ui_managerwidget.h"

ManagerWidget::ManagerWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ManagerWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("管理员端");
    this->model=new QStandardItemModel;
    this->setM=new modelSet;
    this->ui->tableView->setModel(model);
    this->file1=new file;
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    connect(ui->tableView->horizontalHeader(),SIGNAL(sectionDoubleClicked(int)),this, SLOT(sortView(int)));
    this->ui->box_select->hide();
    this->ui->btn_confirm->hide();
    this->ui->btn_exit_check->hide();
    this->ui->edit_check->hide();
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
    this->ui->btn_confirm_2->hide();
    this->ui->btn_exit_2->hide();
    this->addView1=new managerAddNewCourse;
    this->addView2=new managerAddNewPerson;
    this->modifyView1=new managerModifyCourse;
    this->modifyView2=new managerModifyPerson;
    ui->edit_add_course_seclectId->hide();
    ui->label->hide();
    ui->label_2->hide();
    ui->edit_add_courseId->hide();
    ui->label_3->hide();
    ui->spin_class->hide();
    ui->btn_confirm_3->hide();
    ui->btn_refuse->hide();
    ui->btn_quit->hide();
}

ManagerWidget::~ManagerWidget()
{
    delete ui;
}

void ManagerWidget::getLoginId(QString loginId)
{
    this->loginId=loginId;
}

void ManagerWidget::setManageObject()
{
    QStringList adminLine;
    this->file1->readFile("manager.csv",adminLine);
    for(int i=0;i<adminLine.length();i++)
    {
        QString line=adminLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        if(loginId==linesplit.at(1))
        {
            this->managerInfo=new manager;
            this->managerInfo->setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2));
            this->ui->adminName->setText("欢迎"+this->managerInfo->getName()+"登陆系统！");
        }
    }
}


//重写排序算法
void ManagerWidget::sortView(int a)
{
    if(flag)
        this->setM->quickSort(0, model->rowCount()-1,a,model);
    else
        this->setM->quickSort_1(0, model->rowCount()-1,a,model);
    flag=!flag;
}

void ManagerWidget::on_btn_check_student_clicked()
{
    this->model->clear();
    this->managerInfo->displayAllStudent(this->model);
    if(this->addView2->getInfo().at(0)!="0")
    {
        this->model->clear();
        QStringList person=this->addView2->getInfo();
        student newStudent;
        newStudent.setInfo(person.at(0),person.at(1),person.at(3),person.at(4),person.at(2),person.at(5),person.at(7),QStringList(("")));
        this->managerInfo->addNewStudent(newStudent);
        this->managerInfo->displayAllStudent(this->model);
        this->addView2->clearUi();
    }

}

void ManagerWidget::on_btn_check_course_info_clicked()
{
    this->managerInfo->displayAllCourse(this->model);
    if(this->addView1->getInfo().at(0)!="0")
    {
        QStringList Course=this->addView1->getInfo();
        course newCourse;
        newCourse.setCourseInfo(Course.at(0),Course.at(1),Course.at(2),Course.at(3),Course.at(4),Course.at(5),Course.at(6),Course.at(7),Course.at(8),Course.at(9),Course.at(10),Course.at(11),"0");
        this->managerInfo->addNewCourse(newCourse);
        this->managerInfo->displayAllCourse(this->model);
        this->addView1->clearUi();
    }
}

void ManagerWidget::on_btn_check_tea_info_clicked()
{
    this->managerInfo->displayAllTeacher(this->model);
    if(this->addView2->getInfo().at(0)!="0")
    {
        QStringList person=this->addView2->getInfo();
        teacher newTeacher;
        newTeacher.setInfo(person.at(0),person.at(1),person.at(7),person.at(2),person.at(6),QStringList(("")));
        this->managerInfo->addNewTeacher(newTeacher);
        this->managerInfo->displayAllTeacher(this->model);
        this->addView2->clearUi();
    }
}

void ManagerWidget::on_btn_exit_clicked()
{
    managerInfo->saveAllInfo();
    this->close();
}

void ManagerWidget::on_btn_check_info_clicked()
{
    this->ui->box_select->show();
    this->ui->btn_confirm->show();
    this->ui->btn_exit_check->show();
    this->ui->edit_check->show();
}

void ManagerWidget::on_btn_add_course_clicked()
{
    //this->addView1->exec();
    this->addView1->show();
    this->addView1->clearUi();
}


//记得清空
void ManagerWidget::on_btn_add_stu_or_tea_info_clicked()
{
    //this->addView2->exec();
    this->addView2->show();
    this->addView2->clearUi();


}


void ManagerWidget::on_btn_modify_stu_or_tea_info_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(this->managerInfo->whoAreYou(model->item(row,1)->text())!=0)
        {
            QString id=this->model->item(row,1)->text();
            if(this->managerInfo->whoAreYou(id)==2)
            {
                this->modifyView2->getOldInfo(this->managerInfo->findStudentWithId(id).getAllInfo(),this->managerInfo->whoAreYou(id));
            }
            else if(this->managerInfo->whoAreYou(id)==1)
            {
                this->modifyView2->getOldInfo(this->managerInfo->findTeacherWithId(id).getAllInfo(),this->managerInfo->whoAreYou(id));
            }
            //this->modifyView2->exec();
            this->modifyView2->show();
            if(this->modifyView2->getNewInfo().at(0)!="0")
            {
                if(this->managerInfo->whoAreYou(id)==2)
                {
                    this->managerInfo->modifyStudentInfo(this->managerInfo->findStudentWithId(id),this->modifyView2->getNewInfo());
                    this->on_btn_check_student_clicked();
                }
                else if(this->managerInfo->whoAreYou(id)==1)
                {
                    this->managerInfo->modifyTeacherInfo(this->managerInfo->findTeacherWithId(id),this->modifyView2->getNewInfo());
                    this->on_btn_check_tea_info_clicked();
                }
            }
            this->modifyView2->clearUi();
        }
        else
        {
            QMessageBox::critical(this,"错误","请先选择相应信息！","确认");
        }

    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应信息！","确认");
    }

}

void ManagerWidget::on_btn_modify_course_info_clicked()
{

    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(this->managerInfo->whoAreYou(model->item(row,1)->text())==0)
        {
            QString id=this->model->item(row,0)->text();
            this->modifyView1->getOldInfo(this->managerInfo->findCourseWithId(id).getAllInfo());
            this->modifyView1->show();
            //this->modifyView1->exec();
            if(this->modifyView1->getNewInfo().at(0)!="0")
            {
                this->managerInfo->modifyCourseInfo(this->managerInfo->findCourseWithId(id),this->modifyView1->getNewInfo());
                this->on_btn_check_course_info_clicked();
            }
            this->modifyView1->clearUi();
        }
        else
        {
            QMessageBox::critical(this,"错误","请先选择相应信息！","确认");
        }

    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应信息！","确认");
    }
}

void ManagerWidget::on_btn_confirm_clicked()
{
    this->model->clear();
    QString edit=this->ui->edit_check->text();
    int flag=this->ui->box_select->currentIndex();
    switch(flag)
    {
    case 0:
        if(!managerInfo->displayStudentWithName(edit,model))
            QMessageBox::critical(this,"错误","未找到！请检查！","确认");
        break;
    case 1:
        if(!managerInfo->displayStudentWithId(edit,model))
            QMessageBox::critical(this,"错误","未找到！请检查！","确认");
        break;
    case 2:
        if(!managerInfo->displayTeacherWithName(edit,model))
            QMessageBox::critical(this,"错误","未找到！请检查！","确认");
        break;
    case 3:
        if(!managerInfo->displayTeacherWithId(edit,model))
            QMessageBox::critical(this,"错误","未找到！请检查！","确认");
        break;
    case 4:
        if(!managerInfo->displayCourseWithName(edit,model))
            QMessageBox::critical(this,"错误","未找到！请检查！","确认");
        break;
    case 5:
        if(!managerInfo->displayCourseWithId(edit,model))
            QMessageBox::critical(this,"错误","未找到！请检查！","确认");
        break;
    }
}

void ManagerWidget::on_btn_exit_check_clicked()
{
    this->ui->box_select->hide();
    this->ui->btn_confirm->hide();
    this->ui->btn_exit_check->hide();
    this->ui->edit_check->hide();
}

void ManagerWidget::on_btn_modify_password_clicked()
{
    this->ui->oldPassword->show();
    this->ui->newPassword->show();
    this->ui->btn_confirm_2->show();
    this->ui->btn_exit_2->show();
}

void ManagerWidget::on_btn_exit_2_clicked()
{
    this->ui->btn_confirm_2->hide();
    this->ui->btn_exit_2->hide();
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
}

void ManagerWidget::on_btn_confirm_2_clicked()
{
    QString oldPassword=this->ui->oldPassword->text();
    QString newPassword=this->ui->newPassword->text();
    if(this->managerInfo->modifyPassword(oldPassword,newPassword))
    {
        if(QMessageBox::information(this,"修改成功！","密码修改成功","确认")==0)
        {
            this->ui->btn_confirm_2->hide();
            this->ui->btn_exit_2->hide();
            this->ui->oldPassword->hide();
            this->ui->newPassword->hide();
        }
    }
    else
    {
        QMessageBox::critical(this,"修改失败！","旧密码输入错误或新密码不足六位！","确认");
    }
}

void ManagerWidget::on_btn_check_tea_require_clicked()
{
    ui->edit_add_course_seclectId->show();
    ui->label->show();
    ui->label_2->show();
    ui->edit_add_courseId->show();
    ui->label_3->show();
    ui->spin_class->show();
    ui->btn_confirm_3->show();
    ui->btn_refuse->show();
    ui->btn_quit->show();
    this->model->clear();
    this->managerInfo->displayWaitToLoadCourse(model);
}


void ManagerWidget::on_btn_confirm_3_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(ui->edit_add_course_seclectId->text()!=""&&ui->edit_add_courseId->text()!="")
        {
            QStringList course=this->managerInfo->getWaitToLoadCourse()[row].getAllInfo();
            QString temp=QString::number(ui->spin_class->value());
            this->managerInfo->getWaitToLoadCourse()[row].setCourseInfo(ui->edit_add_course_seclectId->text(),ui->edit_add_courseId->text(),course.at(2),temp,course.at(4),course.at(5),course.at(6),course.at(7),course.at(8),course.at(9),course.at(10),course.at(11),"0");
            this->managerInfo->addNewCourse(this->managerInfo->getWaitToLoadCourse()[row]);
            this->managerInfo->getWaitToLoadCourse().removeAt(row);
            this->managerInfo->findTeacherWithName(model->item(row,7)->text()).addCourse(ui->edit_add_course_seclectId->text());
            if(QMessageBox::information(this,"提示","添加课程成功！","确认")==0)
            {
                on_btn_check_course_info_clicked();
                ui->edit_add_course_seclectId->hide();
                ui->label->hide();
                ui->label_2->hide();
                ui->edit_add_courseId->hide();
                ui->label_3->hide();
                ui->spin_class->hide();
                ui->btn_confirm_3->hide();
                ui->btn_refuse->hide();
                ui->btn_quit->hide();
            }
        }
        else
        {
            QMessageBox::critical(this,"错误","信息填写不完整，请重新填写！","确认");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
}

void ManagerWidget::on_btn_refuse_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(QMessageBox::information(this,"提示","是否拒绝开课请求？","确认","取消")==0)
        {
            this->managerInfo->getWaitToLoadCourse().removeAt(row);
            on_btn_check_tea_require_clicked();
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
}

void ManagerWidget::on_btn_quit_clicked()
{
    ui->label->hide();
    ui->label_2->hide();
    ui->edit_add_courseId->hide();
    ui->edit_add_course_seclectId->hide();
    ui->label_3->hide();
    ui->spin_class->hide();
    ui->btn_confirm_3->hide();
    ui->btn_refuse->hide();
    ui->btn_quit->hide();
}

void ManagerWidget::on_btn_delete_info_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(QMessageBox::information(this,"提示","确认删除？","确认","取消")==0)
        {
            QString id=this->model->item(row,1)->text();
            if(this->managerInfo->whoAreYou(id)==2)
            {
                this->managerInfo->deleteStudentInfo(id);
                this->on_btn_check_student_clicked();
            }
            else if(this->managerInfo->whoAreYou(id)==1)
            {
                this->managerInfo->deleteTeacherInfo(id);
                this->on_btn_check_tea_info_clicked();
            }
            else
            {
                this->managerInfo->deleteCourseInfo((this->model->item(row,0)->text()));
                this->on_btn_check_course_info_clicked();
            }
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应信息！","确认");
    }
}
